package cn.dataling.ai.common;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @coder taoym
 * @datetime 2021/10/5 13:36
 * @desc 正则表达式工具类
 */
public class RegexUtils {

    /**
     * @coder taoym
     * @datetime 2021/10/4 21:36
     * @desc 正则表达式 提取字符串
     */
    public static String matcher(String regex, String str) {
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(str);
        String result = "";
        while (matcher.find()) {
            result = matcher.group().trim();
        }
        return result;
    }

    /**
     * @coder taoym
     * @datetime 2021/10/4 21:36
     * @desc 正则表达式 提取字符串
     */
    public static String matcher(String regex, String str, Integer index) {
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(str);
        String result = "";
        while (matcher.find()) {
            // group 0指整个字符串 1指第一个括号里的字符串 2指第二个括号里的字符串
            result = matcher.group(index).trim();
        }
        return result;
    }
}
